<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 'on');
        
/**
 * Description of ClienteController
 *
 * @author Cesar
 */
class Pdfs_PdfsController extends Zend_Controller_Action{
    private $clienteDao;
    private $usuarioDao;
    private $referenciaDao;
    private $ingresoDao;
    private $tipoSesionDao;
    private $tipoDocumentoDao;
    private $sesionDao;
    private $menu = 1;


    public function init() {
        $auht = Zend_Auth::getInstance();
        
        if ($auht->hasIdentity()){
            $this->view->usuarioSesion = $auht->getIdentity();
            //var_dump($auht->getIdentity()->id);
        } else {
            return $this->_redirect("/default/index/login/");
        }
//        if (!Zend_Auth::getInstance()->hasIdentity()){
//            return $this->_redirect("/default/index/login/");
//        }
            
        $this->clienteDao = new Doctrine_Model_ClienteDao();
        $this->usuarioDao = new Doctrine_Model_UsuarioDao();
        $this->referenciaDao = new Doctrine_Model_ReferenciasDoctrineDao();
        $this->ingresoDao = new Doctrine_Model_IngresoDao();
        $this->tipoSesionDao = new Doctrine_Model_TipoSesionDao();
        $this->tipoDocumentoDao = new Doctrine_Model_TipoDocumentoDao();
        $this->sesionDao = new Doctrine_Model_SesionesDao();
        
        $this->view->usuario = $this->usuarioDao->obtenerPorId($auht->getIdentity()->id); 
        
    }
    
    public function indexAction() {
        
        $this->_helper->redirector('index','index', 'default' );
    }
    
    public function generarpdfclientesAction(){
        $cliente = new Doctrine_Model_Entities_Cliente();
        
        $array_clientes = array();
        $datos = $this->getRequest()->getParams();
        $ids_clientes = explode(",",$datos['ids']);
        
        for ($i = 0; $i <= count($ids_clientes)-2; $i++) {
            $cliente = $this->clienteDao->obtenerPorId($ids_clientes[$i]);
            $array_clientes[]= $cliente;
        }
        $this->_helper->layout->disableLayout();
        // create PDF
        $pdf = new Zend_Pdf();
        //$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
        //$image = Zend_Pdf_Image::imageWithPath('imagenes/logoBlanco.jpg');

        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
        $page = new Zend_Pdf_Page("A4");
//        $ancho  = $page->getWidth();
//        $alto = $page->getHeight();
        
      
        $val = 0;
        $c = 0;
        $f = 800; 
        $page->setFont($font, 22)
                    ->setFillColor(Zend_Pdf_Color_Html::color('#89620F'))
                    ->drawText('SUNBATHE', $c + 75, $f);
        $f = $f -60;
        $page->drawLine(50, 735, 500, 735);
        $page->setFont($font, 13)
            ->setFillColor(Zend_Pdf_Color_Html::color('#0C0C0D'))
            ->drawText("Ingreso", $c + 50, $f)
            ->drawText("Nombre", $c + 150, $f)
            ->drawText("Fec. Nacimiento", $c + 300, $f)
            ->drawText("Sexo", $c + 450, $f);
        //$f = $f-10;

        foreach ($array_clientes as $cliente) {
            $f = $f - 30;
            //$page = new Zend_Pdf_Page("595:250");

            if ($cliente->getSexo() == 1) {
                $sexo = 'F';
            } else {
                $sexo = 'M';
            }

            $page->setFont($font, 11)
                    ->setFillColor(Zend_Pdf_Color_Html::color('#220A0A'))
                    ->drawText("{$cliente->getFechaPrimerIngreso()->format("d-m-Y")}", $c + 50, $f)
                    ->drawText("{$cliente->getApellido()}", $c + 150, $f)
                    ->drawText("{$cliente->getNombre()}", $c + 220, $f)
                    ->drawText("{$cliente->getFechaNacimiento()->format("d-m-Y")}", $c + 300, $f)
                    ->drawText($sexo, $c + 450, $f);
            for ($index = 0; $index <= 450; $index++) {
                $page->setFont($font, 4)
                        ->setFillColor(Zend_Pdf_Color_Html::color('#0C0C0D'))
                        ->drawText('-', 50 + $index, $f - 10);
            }
            if ($f <= 100) {
                $f = 800;
                $page->setFont($font, 12)
                            ->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
                            ->drawText(utf8_decode('© SunBathe 2013'), 400, 50 );
                $pdf->pages[] = $page;
                $page = new Zend_Pdf_Page("A4");
            }
        }
        //<p>© SunBathe 2013</p>
         $totalCliente = count($array_clientes);
         $page->setFont($font, 12)
                        ->setFillColor(Zend_Pdf_Color_Html::color('#0A1682'))
                        ->drawText('Total:   '."{$totalCliente}".'  clientes', $c + 50, $f-30);
        $page->setFont($font, 12)
                    ->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'))
                    ->drawText(utf8_decode('© SunBathe 2013'), 400, 50 );
        
        $pdf->pages[] = $page;
        $fecha = new DateTime();
        $filename = "clientes_{$fecha->format("Ymd")}";
        
        $this->getResponse()->setHeader('Content-type', 'application/pdf', true);
        $this->getResponse()->setHeader('Content-disposition', "inline; filename={$filename}.pdf", true);
        $this->getResponse()->setBody($pdf->render());
       // $pdf->save($filename);
    }
    
}